package com.hx.service;

import com.hx.entry.CangKuImportDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.List;

@Service
@RequiredArgsConstructor
public class CangKuService {

    private final JdbcTemplate jdbcTemplate;

    @Value("${spring.datasource.druid.batch-size:50000}")
    private int batchSize;


    private static final String INSERT_SQL = "INSERT INTO cangku (code, name,zd,jz,cang_ku_date) VALUES (?, ?,?, ?,?)";

    @Transactional(propagation = Propagation.NOT_SUPPORTED)
    public void batchInsert(List<CangKuImportDTO> list) {
        jdbcTemplate.batchUpdate(INSERT_SQL, new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int i) throws SQLException {
                CangKuImportDTO item = list.get(i);
                ps.setLong(1, item.getCode());
                ps.setString(2, item.getName());
                ps.setDouble(3, item.getZd());
                ps.setDouble(4, item.getJz());
                ps.setTimestamp(5, new Timestamp(item.getCangKuDate().getTime()));
//                ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
//                ps.setString(7, item.getProvince());
//                ps.setInt(8, item.getProvinceId());
//                ps.setString(9, item.getCity());
//                ps.setInt(10, item.getCityId());
//                ps.setString(11, item.getCounty());
//                ps.setInt(12, item.getCountyId());
            }

            @Override
            public int getBatchSize() {
                return list.size();
            }
        });
    }
}